<?php

namespace App\Admin\Forms;

use App\Models\SwtGood;
use App\Models\SwtTyGood;
use App\Services\TuoYuanService;
use Dcat\Admin\Widgets\Form;

class GetTyGoodsForm extends Form
{
    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {
        $res = SwtTyGood::where('bar_code', $input['bar_code'])
            -> where('is_standard', 1)
            -> first();

        if($res) {
             return $this->response()->error('商品已在模型库中');
        }

        $res = SwtGood::where('bar_code', $input['bar_code'])
            -> first();

        if($res) {
            return $this->response()->error('商品已在商品中');
        }

        $res = TuoYuanService::getGoodsBySku([$input['bar_code']]);

        if($res[$input['bar_code']]) {
            return $this->response()->success('已在库，请新增')->redirect('swt_good/create?bar_code='.$input['bar_code']);
        } else {
            return $this->response()->info('未在库，请建模')->redirect('ty/swt_ty_good/create');
        }
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $this->text('bar_code')->required();
    }

    /**
     * The data of the form.
     *
     * @return array
     */
    public function default()
    {
        return [];
    }
}
